package com.moleq.dialog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.adapter.SelectionAdapter;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.itemsetting.tools.MGCreateFiles;
import com.moleq.logic.MoleQConstants;
import com.moleq.mgpos.activities.FunctionSettingActivity;
import com.moleq.models.FunctionPanel;
import com.moleq.models.GlobalInfo;
import com.moleq.models.SetFunction;
import com.moleq.mgpos.activities.R;
import com.moleq.service.FunctionPanelService;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class SelectFuncDetailDialog extends Dialog implements OnResultListener {
	
	private static int FLAG_TAX_NOTAX = 1;
	private static int FLAG_DEP = 2;
	private static int FLAG_MULTCOUPON = 3;
	private FunctionPanelService mFunctionPanelService;
	private FunctionPanel mFunctionPanel;
	String appr_fl = "0";
	String func_label,p1,func_name,func_label2;
	int p2,func_key,func_no,func_order;
	double p3;
	
	private Context mContext;
	private FunctionSettingActivity fsa; 
	private Button btn_dialog_ok,btn_dialog_cancel,btn_setValue,btn_appr,btn_setDep,btn_setMultCoupon;
	private TextView tv_old_func_name,tv_parameter;
	private EditText et_label,et_parameter1,et_parameter2,et_parameter3,et_label2;
	private AbsoluteLayout al_dialog_main;
	private SelectFuncSourceDialog mSelectFuncSourceDialog;
	private ListView lv_dialog_list;
	private SelectionAdapter apprAdapter = null;
	private List<String> apprSource = new ArrayList<String>();
	
	Date now = new Date();
	private MGCreateFiles mMGCreateFiles = new MGCreateFiles();
	
	public SelectFuncDetailDialog(Context context,FunctionPanel mFunctionPanel){
		super(context,R.style.dialog2);
		constructor(context);
		fsa = (FunctionSettingActivity) context;
		this.mFunctionPanel = mFunctionPanel;
	}
	
	private void constructor(Context context) {
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.fp_dialog_set_detail);
		this.setCancelable(false);
		
		al_dialog_main = (AbsoluteLayout) findViewById(R.id.al_dialog_main);
		tv_old_func_name = (TextView) findViewById(R.id.tv_old_func_name);
		btn_appr = (Button) findViewById(R.id.btn_appr);
		et_label = (EditText) findViewById(R.id.et_label);
		et_label2 = (EditText) findViewById(R.id.et_label2);
		tv_parameter = (TextView) findViewById(R.id.tv_parameter);
		et_parameter1 = (EditText) findViewById(R.id.et_parameter1);
		et_parameter2 = (EditText) findViewById(R.id.et_parameter2);
		et_parameter3 = (EditText) findViewById(R.id.et_parameter3);
		
		btn_setValue = (Button) findViewById(R.id.btn_setValue);
		btn_setDep = (Button) findViewById(R.id.btn_setDep);
		btn_setMultCoupon = (Button) findViewById(R.id.btn_setMultCoupon);
		btn_dialog_ok = (Button) findViewById(R.id.btn_dialog_ok);
		btn_dialog_cancel = (Button) findViewById(R.id.btn_dialog_cancel);
		
		btn_appr.setOnClickListener(mOnClickListener);
		al_dialog_main.setOnClickListener(mOnClickListener);
		btn_setValue.setOnClickListener(mOnClickListener);
		btn_setDep.setOnClickListener(mOnClickListener);
		btn_setMultCoupon.setOnClickListener(mOnClickListener);
		btn_dialog_ok.setOnClickListener(mOnClickListener);
		btn_dialog_cancel.setOnClickListener(mOnClickListener);
	}
	
	public void show(){
		super.show();
		mSelectFuncSourceDialog = new SelectFuncSourceDialog(mContext, this);
		
		mFunctionPanelService = ServiceManager.getInstance().mFunctionPanelService;
		func_no = mFunctionPanel.getTp_no();
		func_order = mFunctionPanel.getTp_order();
		appr_fl = mFunctionPanel.getTp_appl_fl();
		func_key = mFunctionPanel.getTp_func_key();
		func_name = mFunctionPanelService.getFuncNameByFuncKey(func_key);
		func_label = mFunctionPanel.getTp_label_file();
		func_label2 = mFunctionPanel.getTp_label_file2();
		p1 = mFunctionPanel.getTp_parameter1();
		p2 = mFunctionPanel.getTp_parameter2();
		p3 = mFunctionPanel.getTp_parameter3();
		
		tv_old_func_name.setText(func_name);
		String temp = "";
		if (appr_fl.equals("0")) {
			temp = "No";
		}else if (appr_fl.equals("1")) {
			temp = "Immediate";
		}else if (appr_fl.equals("2")) {
			temp = "Delay";
		}
		btn_appr.setText(temp);
		et_label.setText(func_label);
		et_label2.setText(func_label2);
		et_parameter1.setText(p1);
		et_parameter2.setText(String.valueOf(p2));
		et_parameter3.setText(String.valueOf(p3));
		
		switch (func_key) {
		case 0:
			break;
		case MoleQConstants.FUNCTION_COUPON:
			String coupon = "Manual (%)";
			if (p1.equals("M")) {
				coupon = "Manual (%)";
			}else if (p1.equals("N")) {
				coupon = "Manual (Amount)";
			}else if (p1.equals("P")) {
				coupon = "%";
			}else if (p1.equals("A")) {
				coupon = "Amount";
			}else {
				p1 = "M";
				coupon = "Manual (%)";
			}
			if (p3 == 0) {
				et_parameter3.setText("");
			}
			btn_setValue.setText(coupon);
			btn_setValue.setTag(p1);
			btn_setValue.setVisibility(View.VISIBLE);
			et_parameter1.setVisibility(View.INVISIBLE);
			et_parameter2.setVisibility(View.INVISIBLE);
			if (p1.equals("P") || p1.equals("A")) {
				et_parameter3.setVisibility(View.VISIBLE);
			}else {
				et_parameter3.setVisibility(View.INVISIBLE);
			}
			break;
		case MoleQConstants.FUNCTION_TARE:
			String tare = "Manual";
			if (p1.equals("U")) {
				tare = "Unit of Tare";
			}else {
				tare = "Manual";
				p1 = "M";
				et_parameter2.setVisibility(View.INVISIBLE);
			}
			if (p2 == 0) {
				et_parameter2.setText("");
			}
			btn_setValue.setText(tare);
			btn_setValue.setTag(p1);
			btn_setValue.setVisibility(View.VISIBLE);
			et_parameter1.setVisibility(View.INVISIBLE);
			et_parameter3.setVisibility(View.INVISIBLE);
			break;
		case MoleQConstants.FUNCTION_GLOBAL_DISCOUNT:
			String globalName = "";
			if (p2 == 0) {
				globalName = "Manual";
			}else if (p2 == -1) {
				globalName = "Remove";
			}else {
				globalName = ServiceManager.getInstance().mGlobalDiscountSettingService.getGlobalDiscountName(String.valueOf(p2));
			}
			btn_setValue.setText(globalName);
			btn_setValue.setTag(p2);
			btn_setValue.setVisibility(View.VISIBLE);
			et_parameter1.setVisibility(View.INVISIBLE);
			et_parameter2.setVisibility(View.INVISIBLE);
			et_parameter3.setVisibility(View.INVISIBLE);
			break;
		case MoleQConstants.FUNCTION_ITEM_DISOCUNT:
			String itemName = "";
			if (p2 == 0) {
				itemName = "Manual (%)";
			}else if (p2 == -1) {
				itemName = "Manual (Amount)";
			}else {
				itemName = ServiceManager.getInstance().mDiscountSettingService.getItemDiscountName(String.valueOf(p2));
			}
			btn_setValue.setText(itemName);
			btn_setValue.setTag(p2);
			btn_setValue.setVisibility(View.VISIBLE);
			et_parameter1.setVisibility(View.INVISIBLE);
			et_parameter2.setVisibility(View.INVISIBLE);
			et_parameter3.setVisibility(View.INVISIBLE);
			break;
		case MoleQConstants.FUNCTION_SHIFT_TAX:
		case MoleQConstants.FUNCTION_TAX_EXEMPT:
			String tax = "";
			for (int i = 0; i < 7; i++) {
				if (p2 == i) {
					tax = "Tax " + i;
				}
			}
			if (tax.equals("Tax 0")) {
				tax = "All";
			}
			btn_setValue.setText(String.valueOf(tax));
			btn_setValue.setTag(p2);
			btn_setValue.setVisibility(View.VISIBLE);
			et_parameter1.setVisibility(View.INVISIBLE);
			et_parameter2.setVisibility(View.INVISIBLE);
			et_parameter3.setVisibility(View.INVISIBLE);
			break;
		case MoleQConstants.FUNCTION_CVV:
		case MoleQConstants.FUNCTION_WIC:
			String cvv = "";
			if (p2 == 0) {
				cvv = "New/Edit";
			}else if (p2 == -1) {
				cvv = "Remove";
			}
			btn_setValue.setText(cvv);
			btn_setValue.setTag(p2);
			btn_setValue.setVisibility(View.VISIBLE);
			et_parameter1.setVisibility(View.INVISIBLE);
			et_parameter2.setVisibility(View.INVISIBLE);
			et_parameter3.setVisibility(View.INVISIBLE);
			break;
		case MoleQConstants.FUNCTION_VENDER_COUPON:
			String vender = "",dep = "",mult = "";
//			if (p1.equals("T")) {
//				vender = "Tax";
//				p1 = "T";
//			}else {
//				vender = "No Tax";
//				p1 = "N";
//			}
			if (p2 == 0) {
				dep = "Department";
			}else {
				dep = ServiceManager.getInstance().mDepartmantService.getDepName(String.valueOf(p2));
			}
			if (p3 == 0) {
				mult = "Mult. Coupon";
			}else {
				mult = ServiceManager.getInstance().mMultCouponSettingService.getMultName(String.valueOf(p3));
			}
//			btn_setValue.setText(vender);
//			btn_setValue.setTag(p1);
			btn_setDep.setText(dep);
			btn_setDep.setTag(p2);
			btn_setMultCoupon.setText(mult);
			btn_setMultCoupon.setTag(p3);
			btn_setValue.setVisibility(View.INVISIBLE);
			et_parameter1.setVisibility(View.INVISIBLE);
			et_parameter2.setVisibility(View.INVISIBLE);
			et_parameter3.setVisibility(View.INVISIBLE);
			btn_setDep.setVisibility(View.VISIBLE);
			btn_setMultCoupon.setVisibility(View.VISIBLE);
			break;
		default:
			tv_parameter.setVisibility(View.INVISIBLE);
			et_parameter1.setVisibility(View.INVISIBLE);
			et_parameter2.setVisibility(View.INVISIBLE);
			et_parameter3.setVisibility(View.INVISIBLE);
			break;
		}
	}
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {	
			String value = "";
			List<SetFunction> list = new ArrayList<SetFunction>();
			switch (v.getId()) {
			case R.id.btn_setValue:
				switch (func_key) {
				case MoleQConstants.FUNCTION_COUPON:
					list.add(new SetFunction("M", "Manual (%)"));
					list.add(new SetFunction("N", "Manual (Amount)"));
					list.add(new SetFunction("P", "%"));
					list.add(new SetFunction("A", "Amount"));
					value = p1;
					break;
				case MoleQConstants.FUNCTION_GLOBAL_DISCOUNT:
					list = ServiceManager.getInstance().mGlobalDiscountSettingService.FindGlobalDisNoDesc();
					value = String.valueOf(p2);
					break;
				case MoleQConstants.FUNCTION_ITEM_DISOCUNT:
					list = ServiceManager.getInstance().mDiscountSettingService.FindItemDisNoDesc();
					value = String.valueOf(p2);
					break;
				case MoleQConstants.FUNCTION_TARE:
					list.add(new SetFunction("M", "Manual"));
					list.add(new SetFunction("U", "Unit of Tare"));
					value = p1;
					break;
				case MoleQConstants.FUNCTION_SHIFT_TAX:
					list.add(new SetFunction("1", "Tax 1"));
					list.add(new SetFunction("2", "Tax 2"));
					list.add(new SetFunction("3", "Tax 3"));
					list.add(new SetFunction("4", "Tax 4"));
					list.add(new SetFunction("5", "Tax 5"));
					list.add(new SetFunction("6", "Tax 6"));
					list.add(new SetFunction("0", "All"));
					value = String.valueOf(p2);
					break;
				case MoleQConstants.FUNCTION_TAX_EXEMPT:
					list.add(new SetFunction("0", "All"));
					list.add(new SetFunction("1", "Tax 1"));
					list.add(new SetFunction("2", "Tax 2"));
					list.add(new SetFunction("3", "Tax 3"));
					list.add(new SetFunction("4", "Tax 4"));
					list.add(new SetFunction("5", "Tax 5"));
					list.add(new SetFunction("6", "Tax 6"));
					value = String.valueOf(p2);
					break;
				case MoleQConstants.FUNCTION_CVV:
				case MoleQConstants.FUNCTION_WIC:
					list.add(new SetFunction("0", "New/Edit"));
					list.add(new SetFunction("-1", "Remove"));
					break;
//				case MoleQConstants.FUNCTION_VENDER_COUPON:
//					list.add(new SetFunction("N", "No Tax"));
//					list.add(new SetFunction("T", "Tax"));
//					break;
				default:
					break;
				}
//				if (func_key == MoleQConstants.FUNCTION_VENDER_COUPON) {
//					mSelectFuncSourceDialog.setSource(list,value,FLAG_TAX_NOTAX);
//					mSelectFuncSourceDialog.show();
//				}else {
					mSelectFuncSourceDialog.setSource(list,value);
					mSelectFuncSourceDialog.show();
//				}
				break;
			case R.id.btn_setDep:
				list = ServiceManager.getInstance().mDepartmantService.FindDepNoDesc();
				value = String.valueOf(p2);
				mSelectFuncSourceDialog.setSource(list,value,FLAG_DEP);
				mSelectFuncSourceDialog.show();
				break;
			case R.id.btn_setMultCoupon:
				list = ServiceManager.getInstance().mMultCouponSettingService.FindMultNoDesc();
				value = String.valueOf(p3);
				mSelectFuncSourceDialog.setSource(list,value,FLAG_MULTCOUPON);
				mSelectFuncSourceDialog.show();
				break;
			case R.id.btn_dialog_ok:
				String labelName = et_label.getText().toString();
				String labelName2 = et_label2.getText().toString();
				String appr = btn_appr.getText().toString();
				if (appr.equals("No")) {
					appr = "0";
				}else if (appr.equals("Immediate")) {
					appr = "1";
				}else if (appr.equals("Delay")) {
					appr = "2";
				}
				p1 = et_parameter1.getText().toString();
				String para2 = "",para3 = "";
				para3 = et_parameter3.getText().toString();
				switch (func_key) {
				case MoleQConstants.FUNCTION_COUPON:
				case MoleQConstants.FUNCTION_TARE:
					p1 = btn_setValue.getTag().toString();
					para2 = et_parameter2.getText().toString();
					break;
				case MoleQConstants.FUNCTION_GLOBAL_DISCOUNT:
				case MoleQConstants.FUNCTION_ITEM_DISOCUNT:
				case MoleQConstants.FUNCTION_SHIFT_TAX:
				case MoleQConstants.FUNCTION_TAX_EXEMPT:
				case MoleQConstants.FUNCTION_CVV:
				case MoleQConstants.FUNCTION_WIC:
					para2 = btn_setValue.getTag().toString();
					break;
				case MoleQConstants.FUNCTION_VENDER_COUPON:
//					p1 = btn_setValue.getTag().toString();
					para2 = btn_setDep.getTag().toString();
					para3 = btn_setMultCoupon.getTag().toString();
					break;
				default:
					para2 = et_parameter2.getText().toString();
					break;
				}
				if (!para2.equals("")) {
					p2 = Integer.valueOf(para2);
				}else {
					p2 = 0;
				}
				if (!para3.equals("")) {
					p3 = Double.valueOf(para3);
				}else {
					p3 = 0;
				}
				FunctionPanel fp = new FunctionPanel();
				fp.setTp_no(func_no);
				fp.setTp_order(func_order);
				fp.setTp_func_key(func_key);
				fp.setTp_label_file(labelName);
				fp.setTp_label_file2(labelName2);
				fp.setTp_appl_fl(appr);
				fp.setTp_parameter1(p1);
				fp.setTp_parameter2(p2);
				fp.setTp_parameter3(p3);
				fp.setCreate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
				fp.setUpdate_datetime(MoleQUtil.getDateToString("yyyyMMddHHmmss", now));
			
				boolean result = mFunctionPanelService.UpdateOrInsertFuncPanel(fp);
				if (GlobalInfo.getInstance().isMGNetwork){
					if (mFunctionPanelService.CheckFuncNoInList(func_no)) {
						mMGCreateFiles.createrFile(fp.toMGData("UPDATE"));
					}else {
						mMGCreateFiles.createrFile(fp.toMGData("INSERT"));
					}
				}
				fsa.ShowMessage(result, 3);
				fsa.ModifyChangeAdapter(func_order);
				dismiss();
				break;
			case R.id.btn_dialog_cancel:
				dismiss();
				break;
			case R.id.al_dialog_main:
				InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputMethodManager.hideSoftInputFromWindow(SelectFuncDetailDialog.this
						.getCurrentFocus().getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
				break;
			case R.id.btn_appr:
				String apprfl = btn_appr.getText().toString();
				apprSource.clear();
				if (func_key == MoleQConstants.FUNCTION_SIGN_OUT || func_key == MoleQConstants.FUNCTION_NO_SALE) {
					apprSource.add("No");
					apprSource.add("Immediate");
				}else {
					apprSource.add("No");
					apprSource.add("Immediate");
					apprSource.add("Delay");
				}
				DialogWindows(apprfl);
				break;
			}
		}
	};
	
	Dialog DialogWindow = null;
	private void DialogWindows(String apprfl){
		if (DialogWindow == null) {
			DialogWindow = new Dialog(mContext, R.style.Transparent_dialog);
			DialogWindow.setContentView(R.layout.ps_dialog_string_source);
		}
		lv_dialog_list = (ListView) DialogWindow.findViewById(R.id.lv_dialog_list);
		btn_dialog_ok = (Button) DialogWindow.findViewById(R.id.btn_source_ok);
		btn_dialog_cancel = (Button) DialogWindow.findViewById(R.id.btn_source_cancel);
		
		btn_dialog_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_dialog_cancel.setOnClickListener(OnClickDialogWindowListener);
		lv_dialog_list.setOnItemClickListener(onItemClickDialogWindowListener);
		
		if (apprAdapter == null) {
			apprAdapter = new SelectionAdapter(mContext, apprSource);
			lv_dialog_list.setAdapter(apprAdapter);
		}else {
			apprAdapter.setSource(apprSource);
			apprAdapter.notifyDataSetChanged();
		}
		Locate(apprfl);
		DialogWindow.show();
	}
	
	public void Locate(String value) {
		for (int i = 0; apprSource.size() > i; i++) {
			if (apprSource.get(i).equals(value)) {
				apprAdapter.ScrollToRow(i);
				return;
			}
		}
		apprAdapter.ScrollToRow(0);
	}
	
	private Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_source_ok:
				String value = apprSource.get(apprAdapter.GetSelectedIndex()).toString();
				btn_appr = (Button) findViewById(R.id.btn_appr);
				btn_appr.setText(value);
				DialogWindow.dismiss();
				break;
			case R.id.btn_source_cancel:
				DialogWindow.dismiss();
				break;
			}
		}
	};
	
	public OnItemClickListener onItemClickDialogWindowListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (parent.getId()) {
			case R.id.lv_dialog_list:
				apprAdapter.ScrollToRow(position);
				break;
			}
		}
	};
			
	@Override
	public void onReturnValue(int what,Object obj) {
		// TODO Auto-generated method stub
		SetFunction st = (SetFunction)obj;
//		btn_setValue.setText(st.getName());
//		btn_setValue.setTag(st.getValue());
		switch (func_key) {
		case MoleQConstants.FUNCTION_COUPON:
			if (st.getValue().equals("M") || st.getValue().equals("N")) {
				et_parameter3.setText("");
				et_parameter3.setVisibility(View.INVISIBLE);
			}else {
				et_parameter3.setVisibility(View.VISIBLE);
				et_parameter3.requestFocus();
			}
			btn_setValue.setText(st.getName());
			btn_setValue.setTag(st.getValue());
			break;
		case MoleQConstants.FUNCTION_TARE:
			if (st.getValue().equals("M")) {
				et_parameter2.setText("");
				et_parameter2.setVisibility(View.INVISIBLE);
			}else {
				et_parameter2.setVisibility(View.VISIBLE);
				et_parameter2.requestFocus();
			}
			btn_setValue.setText(st.getName());
			btn_setValue.setTag(st.getValue());
			break;
		case MoleQConstants.FUNCTION_VENDER_COUPON:
			if (what == 1) {
				btn_setValue.setText(st.getName());
				btn_setValue.setTag(st.getValue());
			}else if (what == 2) {
				btn_setDep.setText(st.getName());
				btn_setDep.setTag(st.getValue());
			}else if (what == 3) {
				btn_setMultCoupon.setText(st.getName());
				btn_setMultCoupon.setTag(st.getValue());
			}
			break;
		default:
			btn_setValue.setText(st.getName());
			btn_setValue.setTag(st.getValue());
			break;
		}
		
	}

	@Override
	public void onCloseWithReturn(Class<? extends MoleQBaseDialog> pramClass,
			int viewId, String returnvalue) {
		// TODO Auto-generated method stub
		
	}

	
	@Override
	public void onShowingMessageDlg(boolean isShowing) {
		// TODO Auto-generated method stub
		
	}
}
